package com.ruoyi.web.service.impl;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.domain.ClassSeat;
import com.ruoyi.web.domain.ClassUser;
import com.ruoyi.web.mapper.ClassUserMapper;
import com.ruoyi.web.service.IClassSeatService;
import com.ruoyi.web.service.IClassUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 *  Service业务层处理
 *

 * @date 2022-03-15
 */
@Service
public class ClassUserServiceImpl implements IClassUserService
{
    @Autowired
    private ClassUserMapper classUserMapper;
    @Resource
    private IClassSeatService seatService;
    @Resource
    private ISysUserService userService;

    @Resource
    private SysRoleMapper roleMapper;

    /**
     * 查询
     *
     * @param id  主键
     * @return
     */
    @Override
    public ClassUser selectClassUserById(Long id)
    {
        return classUserMapper.selectClassUserById(id);
    }

    /**
     * 查询 列表
     *
     * @param classUser
     * @return
     */
    @Override
    public List<ClassUser> selectClassUserList(ClassUser classUser)
    {
        List<ClassUser> classUsers = classUserMapper.selectClassUserList(classUser);
        for (ClassUser c: classUsers) {
            SysUser sysUser = userService.selectUserById((long) c.getUserId());
            ClassSeat classSeat = seatService.selectClassSeatById((long) c.getUserId());
            c.setUserName(sysUser.getUserName());
            c.setClassName(classSeat.getClassNum());
        }
        return classUsers;
    }

    /**
     * 新增
     *
     * @param classUser
     * @return 结果
     */
    @Override
    public int insertClassUser(ClassUser classUser)
    {
        return classUserMapper.insertClassUser(classUser);
    }

    @Override
    public void  orderClass(ClassUser classUser) {
        classUser.setStatus(0);
        List<ClassUser> classUsers = classUserMapper.selectClassUserList(classUser);
        if (classUsers.size() != 0){
            throw new BootstrapMethodError("已经预约先结束预约");
        }else {
            ClassSeat classSeat = seatService.selectClassSeatById((long) classUser.getClassId());
            ClassSeat classSeat1 = new ClassSeat();
            // 教室的座位 剩余座位-1 已预约座位+1
            if (classSeat.getRemainder() < classSeat.getTotal()) {
                classSeat1.setId((long) classUser.getClassId());
                classSeat1.setRemainder(classSeat.getRemainder() - 1);
                classSeat1.setReservationMade(classSeat.getReservationMade() + 1);
                seatService.updateClassSeat(classSeat1);
                // 预约信息添加
                classUser.setCreatTime(new Date());
                classUser.setStatus(0);
                classUser.setPrice(classSeat.getHourPrice());
                classUser.setId(null);
                classUserMapper.insertClassUser(classUser);
            }
        }
    }

    @Override
    public void cancalClass(ClassUser classUser) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        Date date = new Date();
        classUser.setStatus(0);
        List<ClassUser> classUsers = classUserMapper.selectClassUserList(classUser);
        if (classUsers.size() == 0){
            throw new BootstrapMethodError("未预约无法取消");
        }
        ClassUser classUser1 = classUsers.get(0);
        Date creatTime = classUser1.getCreatTime();
        long differ = date.getTime()- creatTime.getTime();
        Long d = differ % nd /nh  + 24 * (differ / nd);
        Long price = classUser1.getPrice();
        Long allPerice = d*price;
        classUser1.setStatus(1);
        classUser1.setAllPrice(allPerice.toString());
        classUser1.setUpdateTime(new Date());
        classUserMapper.updateClassUser(classUser1);
    }

    /**
     * 修改
     *
     * @param classUser
     * @return 结果
     */
    @Override
    public int updateClassUser(ClassUser classUser)
    {
        UserRule((long) classUser.getUserId());
        return classUserMapper.updateClassUser(classUser);
    }
    public void UserRule(long userId){
        List<SysRole> perms = roleMapper.selectRolePermissionByUserId(userId);
        if (!"1".equals(perms.get(0).getDataScope())){
            throw new BootstrapMethodError("权限不够不能做此操作");
        }
    }
    /**
     * 批量删除
     *
     * @param ids 需要删除的 主键
     * @return 结果
     */
    @Override
    public int deleteClassUserByIds(Long[] ids)
    {

        for (int i = 0 ; i < ids.length ; i++){
            UserRule(ids[i]);
        }
        return classUserMapper.deleteClassUserByIds(ids);
    }

    /**
     * 删除 信息
     *
     * @param id  主键
     * @return 结果
     */
    @Override
    public int deleteClassUserById(Long id)
    {
        UserRule(id);
        return classUserMapper.deleteClassUserById(id);
    }
}
